{-include file=$smarty.const.B_DIR|cat:'/src/_header.htm'-}

<script type="text/javascript">
Ext.onReady(function() {
	Ext.QuickTips.init();

	var yesno = {-$yesno-};

	var update	= function(){
		if( grid.getBottomToolbar().disabled){
			return false;
		}
		var o = sm.getSelections();
		if(  o.length!== 1 ){						
			var o = {
				title: "注意" ,
				msg: "请选择一条记录!" ,
				buttons: Ext.Msg.OK,
				icon: Ext.MessageBox.ERROR
			};
			Ext.MessageBox.show(o);
			return ;
		}
		window.setTitle('修改原料分类');
		grid.getBottomToolbar().disable();
		window.show();
		form.getForm().setValues(o[0].data);

	}

	var insert = function(){
		grid.getBottomToolbar().disable();
		window. setTitle('添加原料分类');
		window.show();
		form.getForm().reset();
		
	}
	var insertOk = function(){
		if( !form.form.isValid()){
			return false;
		}
		form.form.submit({
			method: 'POST',
			waitMsg: '验证中..',
			success: function(form, action) {
				window.hide();
				store.reload({callback: function(){
					window.hide();
					grid.getBottomToolbar().enable();
				}});
			},
			failure: function(form , action){ //()  response.responseText
				alert("递交信息失败，请联系管理员")
			}
		});
	}

	var store = new Ext.data.JsonStore({
		url: '?act=list',
		root:'root',
		id:'id',
		fields: [
			{name:'id', type:'int'},
			'原料分类',
			{name:'废弃', type:'int'},
			'备注'
		]	
	});
	
	var sm = new Ext.grid.CheckboxSelectionModel(); 
	var grid = new Ext.grid.GridPanel({
		sm:sm,
		store: store,
		columns: [
			{id:'id',header: "ID", width: 40, dataIndex: 'id'},
			{header: "原料分类", width: 95, sortable: true, dataIndex: '原料分类'},
			{header: "备注", width: 560, dataIndex: '备注'},
			{header: "废弃", width: 40, dataIndex: '废弃', renderer: function(val){
				return val ?  '是' : '否';
			}}
		],
		stripeRows: true,
		//autoExpandColumn: '原料分类',
		//title:'原料分类',
		//height:'100%',
		region:'center',
		bbar:[
			{
				text:'添加',
				iconCls:'icon-add',
				handler:insert
			}, '-' ,
			{
				text:'编辑',
				iconCls:'icon-edit',
				handler: update
			},'-', {
				text:'删除',
				iconCls:'icon-delete',
				handler: function(){
					var o = sm.getSelections();
					if( ! o.length ){						
						var o = {
							title: "注意" ,
							msg: "请至少选择一条记录!" ,
							buttons: Ext.Msg.OK,
							icon: Ext.MessageBox.ERROR
						};
						Ext.MessageBox.show(o);
						return ;
					}
					var r = []
					Ext.each(o,function(node){
						r.push(node.data.id);
					});
					Ext.MessageBox.confirm('提示', '您确定要删除第'+ r.join(',') +'条记录吗?', function(ret){
						if( ret !== 'yes' ){
							return false;
						}
						grid.getBottomToolbar().disable();	
						Ext.Ajax.request({
							url: '?act=del',
							success: function(response, options ){
								if(response.responseText !='ok'){
									Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！", function(){
										alert(response.responseText);
									})
								}else{
									store.reload({callback: function(){
										grid.getBottomToolbar().enable();
									}});
								}
							},
							failure: function(response, options){
								Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！")
							},
							params: { 
								dels : r.join(',')
							}
						});
					});
					//grid.getBottomToolbar().disable();
				}
			}, '->', {
				text: '重载数据',
				iconCls:'icon-refresh',
				handler: function(){
					viewport.disable();
					store.reload({
						callback: function(){
							viewport.enable();
						}
					});
				}
			}
		]
	});

	grid.on({
		'celldblclick':update
	});
	
	var viewport = new Ext.Viewport({
		layout:'border',
		items:grid
	});

	viewport.disable();
	store.load({callback: function(){
		viewport.enable();
	}});

	//========================================
	var ds	= new Ext.data.JsonStore({
			url: 'src/js/yesno.js',
			root: 'root',
			fields: ['val', 'text']
		 });
	ds.load();
	var combo = new Ext.form.ComboBox({
		valueField:'val',
		displayField:'text',
		store: ds,
		typeAhead: true,
		mode: 'local',
		triggerAction: 'all',
		emptyText:'请选择..',
		selectOnFocus:true,
		editable: false, 
		fieldLabel: '废弃',
		name: '废弃1',
		hiddenName: '废弃',
		anchor:'44%'
	});

	//========================================
	var form = new Ext.form.FormPanel({
		baseCls: 'x-plain',
		labelWidth: 55,
		url:'?act=save',
		defaultType: 'textfield',
		items: [
			{
				xtype:'hidden',
				name:'id'
			},
			{
				xtype: 'textfield',
				fieldLabel: '原料分类',
				name: '原料分类',
				allowBlank: false,
				minLength:1
			},combo, {
				xtype: 'textarea',
				fieldLabel: '备注',
				name: '备注',
				anchor: '98% -53'  // anchor width by percentage and height by raw adjustment
			}
		]
	});
	
	//var mask	= new Ext.LoadMask( grid.getEl(),{msg:'数据读取中...',removeMask:true});

	var window = new Ext.Window({
		title: '添加原料分类',
		width: 400,
		height:220,
		minWidth: 290,
		minHeight: 100,
		layout: 'fit',
		plain:true,
		bodyStyle:'padding:5px;',
		buttonAlign:'center',
		maskDisabled : false,
		items: form,
		style :'z-index:9999',
		buttons: [
			{
				text: '保存',
				handler: insertOk
			},{
				text: '取消',
				handler: function(){
					window.hide();
					grid.getBottomToolbar().enable();
				}
			}
		]
	});
	window.on({
		 beforeclose  : function(){
			window.hide();
			grid.getBottomToolbar().enable();
			return false;
		}
	});



});

</script>


{-include file=$smarty.const.B_DIR|cat:'/src/_footer.htm'-}